home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / gnused.zip / GNUSED / MALLOC.C < prev    next >
C/C++ Source or Header  |  1992-11-06  |  24KB  |  895 lines

  1. /* dynamic memory allocation for GNU.
  2.    Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22. /* TimF@microsoft.com:    19-MAR-92  Port to Microsoft's Windows NT (tm) */
  23.  
  24.  
  25. /*
  26.  * @(#)nmalloc.c 1 (Caltech) 2/21/82
  27.  *
  28.  *    U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
  29.  *
  30.  *    Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
  31.  *
  32.  * This is a very fast storage allocator.  It allocates blocks of a small 
  33.  * number of different sizes, and keeps free lists of each size.  Blocks
  34.  * that don't exactly fit are passed up to the next larger size.  In this 
  35.  * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
  36.  * This is designed for use in a program that uses vast quantities of
  37.  * memory, but bombs when it runs out.  To make it a little better, it
  38.  * warns the user when he starts to get near the end.
  39.  *
  40.  * June 84, ACT: modified rcheck code to check the range given to malloc,
  41.  * rather than the range determined by the 2-power used.
  42.  *
  43.  * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
  44.  * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
  45.  * You should call malloc_init to reinitialize after loading dumped Emacs.
  46.  * Call malloc_stats to get info on memory stats if MSTATS turned on.
  47.  * realloc knows how to return same block given, just changing its size,
  48.  * if the power of 2 is correct.
  49.  */
  50.  
  51. /*
  52.  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
  53.  * smallest allocatable block is 8 bytes.  The overhead information will
  54.  * go in the first int of the block, and the returned pointer will point
  55.  * to the second.
  56.  *
  57. #ifdef MSTATS
  58.  * nmalloc[i] is the difference between the number of mallocs and frees
  59.  * for a given block size.
  60. #endif MSTATS
  61.  */
  62.  
  63. #ifdef emacs
  64. /* config.h specifies which kind of system this is.  */
  65. #include "config.h"
  66. #include <signal.h>
  67. #else
  68.  
  69. /* Determine which kind of system this is.  */
  70. #include <signal.h>
  71. #ifndef SIGTSTP
  72. #ifndef VMS
  73. #ifndef USG
  74. #define USG
  75. #endif
  76. #endif /* not VMS */
  77. #else /* SIGTSTP */
  78. #ifdef SIGIO
  79. #define BSD4_2
  80. #endif /* SIGIO */
  81. #endif /* SIGTSTP */
  82.  
  83. #endif /* not emacs */
  84.  
  85. /* Define getpagsz () if the system does not.  */
  86. #include "getpagsz.h"
  87.  
  88. #ifdef BSD
  89. #ifdef BSD4_1
  90. #include <sys/vlimit.h>        /* warn the user when near the end */
  91. #else /* if 4.2 or newer */
  92. #include <sys/time.h>
  93. #include <sys/resource.h>
  94. #endif /* if 4.2 or newer */
  95. #endif
  96.  
  97. #ifdef VMS
  98. #include "vlimit.h"
  99. #endif
  100.  
  101. #ifdef    WINDOWSNT
  102. #include    <windows.h>
  103. #endif
  104.  
  105. extern char *start_of_data ();
  106.  
  107. #define ISALLOC ((char) 0xf7)    /* magic byte that implies allocation */
  108. #define ISFREE ((char) 0x54)    /* magic byte that implies free block */
  109.                 /* this is for error checking only */
  110. #define ISMEMALIGN ((char) 0xd6)  /* Stored before the value returned by
  111.                      memalign, with the rest of the word
  112.                      being the distance to the true
  113.                      beginning of the block.  */
  114.  
  115.  
  116. /* These two are for user programs to look at, when they are interested.  */
  117.  
  118. unsigned int malloc_sbrk_used;       /* amount of data space used now */
  119. unsigned int malloc_sbrk_unused;     /* amount more we can have */
  120.  
  121. /* start of data space; can be changed by calling init_malloc */
  122. static char *data_space_start;
  123.  
  124. #ifdef MSTATS
  125. static int nmalloc[30];
  126. static int nmal, nfre;
  127. #endif /* MSTATS */
  128.  
  129. /* If range checking is not turned on, all we have is a flag indicating
  130.    whether memory is allocated, an index in nextf[], and a size field; to
  131.    realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
  132.    on whether the former can hold the exact size (given the value of
  133.    'index').  If range checking is on, we always need to know how much space
  134.    is allocated, so the 'size' field is never used. */
  135.  
  136. struct mhead {
  137.     char     mh_alloc;    /* ISALLOC or ISFREE */
  138.     char     mh_index;    /* index in nextf[] */
  139. /* Remainder are valid only when block is allocated */
  140.     unsigned short mh_size;    /* size, if < 0x10000 */
  141. #ifdef rcheck
  142.     unsigned mh_nbytes;    /* number of bytes allocated */
  143.     int      mh_magic4;    /* should be == MAGIC4 */
  144. #endif /* rcheck */
  145. };
  146.  
  147. /* Access free-list pointer of a block.
  148.   It is stored at block + 4.
  149.   This is not a field in the mhead structure
  150.   because we want sizeof (struct mhead)
  151.   to describe the overhead for when the block is in use,
  152.   and we do not want the free-list pointer to count in that.  */
  153.  
  154. #define CHAIN(a) \
  155.   (*(struct mhead **) (sizeof (char *) + (char *) (a)))
  156.  
  157. #ifdef rcheck
  158.  
  159. /* To implement range checking, we write magic values in at the beginning and
  160.    end of each allocated block, and make sure they are undisturbed whenever a
  161.    free or a realloc occurs. */
  162. /* Written in each of the 4 bytes following the block's real space */
  163. #define MAGIC1 0x55
  164. /* Written in the 4 bytes before the block's real space */
  165. #define MAGIC4 0x55555555
  166. #define ASSERT(p) if (!(p)) botch("p"); else
  167. #define EXTRA  4        /* 4 bytes extra for MAGIC1s */
  168. #else
  169. #define ASSERT(p) if (!(p)) abort (); else
  170. #define EXTRA  0
  171. #endif /* rcheck */
  172.  
  173. /*
  174.  * I hate magic numbers, besides it's more efficient to make an allocation
  175.  * just under 64k on Windows NT(tm) than it is to make a lot of little ones of
  176.  * about 1k each.  (Since we don't have the equivalent of sbrk...)
  177.  * TimF@Microsoft.Com    15-Feb-92
  178.  */
  179. #ifdef    WINDOWSNT
  180. /* near 64k allocation is much more efficient that 1k on WINDOWSNT */
  181. #define    ALLOCATION_SIZE    (64 * 1024 - 64)
  182. #else /* !WINDOWSNT */
  183. #define    ALLOCATION_SIZE    1024
  184. #endif /* !WINDOWSNT */
  185.  
  186. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  187.  
  188. static struct mhead *nextf[30];
  189.  
  190. /* busy[i] is nonzero while allocation of block size i is in progress.  */
  191.  
  192. static char busy[30];
  193.  
  194. /* Number of bytes of writable memory we can expect to be able to get */
  195. static unsigned int lim_data;
  196.  
  197. /* Level number of warnings already issued.
  198.   0 -- no warnings issued.
  199.   1 -- 75% warning already issued.
  200.   2 -- 85% warning already issued.
  201. */
  202. static int warnlevel;
  203.  
  204. /* Function to call to issue a warning;
  205.    0 means don't issue them.  */
  206. static void (*warnfunction) ();
  207.  
  208. /* nonzero once initial bunch of free blocks made */
  209. static int gotpool;
  210.  
  211. char *_malloc_base;
  212.  
  213. static void getpool ();
  214.  
  215. char *malloc ();
  216.  
  217. /* Cause reinitialization based on job parameters;
  218.   also declare where the end of pure storage is. */
  219. void
  220. malloc_init (start, warnfun)
  221.      char *start;
  222.      void (*warnfun) ();
  223. {
  224.   if (start)
  225.     data_space_start = start;
  226.   lim_data = 0;
  227.   warnlevel = 0;
  228.   warnfunction = warnfun;
  229. }
  230.  
  231. /* Return the maximum size to which MEM can be realloc'd
  232.    without actually requiring copying.  */
  233.  
  234. int
  235. malloc_usable_size (mem)
  236.      char *mem;
  237. {
  238.   struct mhead *p
  239.     = (struct mhead *) (mem - ((sizeof (struct mhead) + 7) & ~7));
  240.   int blocksize = 8 << p->mh_index;
  241.  
  242.   return blocksize - sizeof (struct mhead) - EXTRA;
  243. }
  244.  
  245. static void
  246. morecore (nu)            /* ask system for more memory */
  247.      register int nu;        /* size index to get more of  */
  248. {
  249.   char *sbrk ();
  250.   register char *cp;
  251.   register int nblks;
  252.   register unsigned int siz;
  253.   int oldmask;
  254.  
  255. #ifdef BSD
  256. #ifndef BSD4_1
  257.   int newmask = -1;
  258.   /* Blocking these signals interferes with debugging, at least on BSD on
  259.      the HP 9000/300.  */
  260. #ifdef SIGTRAP
  261.   newmask &= ~(1 << SIGTRAP);
  262. #endif
  263. #ifdef SIGILL
  264.   newmask &= ~(1 << SIGILL);
  265. #endif
  266. #ifdef SIGTSTP
  267.   newmask &= ~(1 << SIGTSTP);
  268. #endif
  269. #ifdef SIGSTOP
  270.   newmask &= ~(1 << SIGSTOP);
  271. #endif
  272.   oldmask = sigsetmask (newmask);
  273. #endif
  274. #endif
  275.  
  276. #ifndef    WINDOWSNT
  277.   if (!data_space_start)
  278.     {
  279.       data_space_start = start_of_data ();
  280.     }
  281. #endif    /* WINDOWSNT */
  282.  
  283.   if (lim_data == 0)
  284.     get_lim_data ();
  285.  
  286.  /*
  287.   * On initial startup, get two blocks of each size up to ALLOCATION_SIZE
  288.   * bytes
  289.   */
  290.   if (!gotpool)
  291.     { getpool (); getpool (); gotpool = 1; }
  292.  
  293.   /* Find current end of memory and issue warning if getting near max */
  294.  
  295. #ifndef    WINDOWSNT
  296. #ifndef VMS
  297.   /* Maximum virtual memory on VMS is difficult to calculate since it
  298.    * depends on several dynmacially changing things. Also, alignment
  299.    * isn't that important. That is why much of the code here is ifdef'ed
  300.    * out for VMS systems.
  301.    */
  302.   cp = sbrk (0);
  303.   siz = cp - data_space_start;
  304.  
  305.   if (warnfunction)
  306.     switch (warnlevel)
  307.       {
  308.       case 0: 
  309.     if (siz > (lim_data / 4) * 3)
  310.       {
  311.         warnlevel++;
  312.         (*warnfunction) ("Warning: past 75% of memory limit");
  313.       }
  314.     break;
  315.       case 1: 
  316.     if (siz > (lim_data / 20) * 17)
  317.       {
  318.         warnlevel++;
  319.         (*warnfunction) ("Warning: past 85% of memory limit");
  320.       }
  321.     break;
  322.       case 2: 
  323.     if (siz > (lim_data / 20) * 19)
  324.       {
  325.         warnlevel++;
  326.         (*warnfunction) ("Warning: past 95% of memory limit");
  327.       }
  328.     break;
  329.       }
  330.  
  331.   if ((int) cp & (ALLOCATION_SIZE - 1))    /* land on ALLOCATION_SIZE boundaries */
  332.     sbrk (ALLOCATION_SIZE - ((int) cp & (ALLOCATION_SIZE - 1)));
  333. #endif /* not VMS */
  334. #endif /* WINDOWSNT */
  335.  
  336.   /* Take at least 2k, and figure out how many blocks of the desired size
  337.      we're about to get */
  338.   nblks = 1;
  339.   if ((siz = nu) < 8)
  340.     nblks = 1 << ((siz = 8) - nu);
  341.  
  342. #ifdef    WINDOWSNT
  343.   if (!(cp = (char *)GlobalAlloc(GMEM_FIXED | GMEM_NOCOMPACT, 1 << (siz + 3))))
  344. #else /* !WINDOWSNT */
  345.   if ((cp = sbrk(1 << (siz + 3))) == (char *)-1)
  346. #endif /* !WINDOWSNT */
  347.     {
  348. #ifdef BSD
  349. #ifndef BSD4_1
  350. #ifndef    WINDOWSNT
  351.       sigsetmask (oldmask);
  352. #endif    /* !WINDOWSNT */
  353. #endif
  354. #endif
  355.       return;            /* no more room! */
  356.     }
  357.   malloc_sbrk_used = siz;
  358.   malloc_sbrk_unused = lim_data - siz;
  359.  
  360. #ifndef VMS
  361.   if ((int) cp & 7)
  362.     {        /* shouldn't happen, but just in case */
  363.       cp = (char *) (((int) cp + 8) & ~7);
  364.       nblks--;
  365.     }
  366. #endif /* not VMS */
  367.  
  368.  /* save new header and link the nblks blocks together */
  369.   nextf[nu] = (struct mhead *) cp;
  370.   siz = 1 << (nu + 3);
  371.   while (1)
  372.     {
  373.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  374.       ((struct mhead *) cp) -> mh_index = nu;
  375.       if (--nblks <= 0) break;
  376.       CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
  377.       cp += siz;
  378.     }
  379.   CHAIN ((struct mhead *) cp) = 0;
  380.  
  381. #ifdef BSD
  382. #ifndef BSD4_1
  383.   sigsetmask (oldmask);
  384. #endif
  385. #endif
  386. }
  387.  
  388. static void
  389. getpool ()
  390. {
  391.   register int nu;
  392.   char * sbrk ();
  393. #ifdef WINDOWSNT
  394.     register char *cp;
  395. /* What assumptions are made about the memory between _malloc_base and
  396.    sbrk(0)? (TimF@microsoft.com) */
  397. #else /* !WINDOWSNT */
  398.   register char *cp = sbrk (0);
  399.  
  400.   if ((int) cp & (ALLOCATION_SIZE - 1))    /* land on ALLOCATION_SIZE boundaries */
  401.     sbrk (ALLOCATION_SIZE - ((int) cp & (ALLOCATION_SIZE - 1)));
  402.  
  403.   /* Record address of start of space allocated by malloc.  */
  404.   if (_malloc_base == 0)
  405.     _malloc_base = cp;
  406. #endif /* !WINDOWSNT */
  407.  
  408.   /* Get (ALLOCATION_SIZE * 2) bytes of storage */
  409. #ifdef    WINDOWSNT
  410.     cp = (char *)GlobalAlloc(GMEM_FIXED | GMEM_NOCOMPACT, ALLOCATION_SIZE);
  411.  
  412.     if (!cp)
  413.         return;
  414. #else /* !WINDOWSNT */
  415.   cp = sbrk (ALLOCATION_SIZE * 2);
  416.   if (cp == (char *) -1)
  417.     return;
  418. #endif /* !WINDOWSNT */
  419.  
  420.   /* Divide it into an initial 8-word block
  421.      plus one block of size 2**nu for nu = 3 ... 10.  */
  422.  
  423.   CHAIN (cp) = nextf[0];
  424.   nextf[0] = (struct mhead *) cp;
  425.   ((struct mhead *) cp) -> mh_alloc = ISFREE;
  426.   ((struct mhead *) cp) -> mh_index = 0;
  427.   cp += 8;
  428.  
  429.   for (nu = 0; nu < 7; nu++)
  430.     {
  431.       CHAIN (cp) = nextf[nu];
  432.       nextf[nu] = (struct mhead *) cp;
  433.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  434.       ((struct mhead *) cp) -> mh_index = nu;
  435.       cp += 8 << nu;
  436.     }
  437. }
  438.  
  439. char *
  440. malloc (n)        /* get a block */
  441.      unsigned n;
  442. {
  443.   register struct mhead *p;
  444.   register unsigned int nbytes;
  445.   register int nunits = 0;
  446.  
  447.   /* Figure out how many bytes are required, rounding up to the nearest
  448.      multiple of 8, then figure out which nestf[] area to use.
  449.      Both the beginning of the header and the beginning of the
  450.      block should be on an eight byte boundary.  */
  451. #ifdef SUNOS_LOCALTIME_BUG
  452.   if (n < 16)
  453.     n = 16;
  454. #endif
  455.   nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
  456.   {
  457.     register unsigned int   shiftr = (nbytes - 1) >> 2;
  458.  
  459.     while (shiftr >>= 1)
  460.       nunits++;
  461.   }
  462.  
  463.   /* In case this is reentrant use of malloc from signal handler,
  464.      pick a block size that no other malloc level is currently
  465.      trying to allocate.  That's the easiest harmless way not to
  466.      interfere with the other level of execution.  */
  467.   while (busy[nunits])
  468.     nunits++;
  469.   busy[nunits] = 1;
  470.  
  471.   /* If there are no blocks of the appropriate size, go get some */
  472.   /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
  473.   if (nextf[nunits] == 0)
  474.     morecore (nunits);
  475.  
  476.   /* Get one block off the list, and set the new list head */
  477.   if ((p = nextf[nunits]) == 0)
  478.     {
  479.       busy[nunits] = 0;
  480.  
  481. DbgPrint("Malloc failing allocation of %d bytes\n", n);
  482.  
  483.       return 0;
  484.     }
  485.   nextf[nunits] = CHAIN (p);
  486.   busy[nunits] = 0;
  487.  
  488.   /* Check for free block clobbered */
  489.   /* If not for this check, we would gobble a clobbered free chain ptr */
  490.   /* and bomb out on the NEXT allocate of this size block */
  491.   if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
  492. #ifdef rcheck
  493.     botch ("block on free list clobbered");
  494. #else /* not rcheck */
  495.     abort ();
  496. #endif /* not rcheck */
  497.  
  498.   /* Fill in the info, and if range checking, set up the magic numbers */
  499.   p -> mh_alloc = ISALLOC;
  500. #ifdef rcheck
  501.   p -> mh_nbytes = n;
  502.   p -> mh_magic4 = MAGIC4;
  503.   {
  504.     /* Get the location n after the beginning of the user's space.  */
  505.     register char *m = (char *) p + ((sizeof *p + 7) & ~7) + n;
  506.  
  507.     *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
  508.   }
  509. #else /* not rcheck */
  510.   p -> mh_size = n;
  511. #endif /* not rcheck */
  512. #ifdef MSTATS
  513.   nmalloc[nunits]++;
  514.   nmal++;
  515. #endif /* MSTATS */
  516.   return (char *) p + ((sizeof *p + 7) & ~7);
  517. }
  518.  
  519. free (mem)
  520.      char *mem;
  521. {
  522.   register struct mhead *p;
  523.   {
  524.     register char *ap = mem;
  525.  
  526.     if (ap == 0)
  527.       return;
  528.  
  529.     p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
  530.     if (p -> mh_alloc == ISMEMALIGN)
  531.       {
  532.     ap -= p->mh_size;
  533.     p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
  534.       }
  535.  
  536. #ifndef rcheck
  537.     if (p -> mh_alloc != ISALLOC)
  538.       abort ();
  539.  
  540. #else /* rcheck */
  541.     if (p -> mh_alloc != ISALLOC)
  542.       {
  543.     if (p -> mh_alloc == ISFREE)
  544.       botch ("free: Called with already freed block argument\n");
  545.     else
  546.       botch ("free: Called with bad argument\n");
  547.       }
  548.  
  549.     ASSERT (p -> mh_magic4 == MAGIC4);
  550.     ap += p -> mh_nbytes;
  551.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
  552.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
  553. #endif /* rcheck */
  554.   }
  555.   {
  556.     register int nunits = p -> mh_index;
  557.  
  558.     ASSERT (nunits <= 29);
  559.     p -> mh_alloc = ISFREE;
  560.  
  561.     /* Protect against signal handlers calling malloc.  */
  562.     busy[nunits] = 1;
  563.     /* Put this block on the free list.  */
  564.     CHAIN (p) = nextf[nunits];
  565.     nextf[nunits] = p;
  566.     busy[nunits] = 0;
  567.  
  568. #ifdef MSTATS
  569.     nmalloc[nunits]--;
  570.     nfre++;
  571. #endif /* MSTATS */
  572.   }
  573. }
  574.  
  575. char *
  576. realloc (mem, n)
  577.      char *mem;
  578.      register unsigned n;
  579. {
  580.   register struct mhead *p;
  581.   register unsigned int tocopy;
  582.   register unsigned int nbytes;
  583.   register int nunits;
  584.  
  585.   if (mem == 0)
  586.     return malloc (n);
  587.   p = (struct mhead *) (mem - ((sizeof *p + 7) & ~7));
  588.   nunits = p -> mh_index;
  589.   ASSERT (p -> mh_alloc == ISALLOC);
  590. #ifdef rcheck
  591.   ASSERT (p -> mh_magic4 == MAGIC4);
  592.   {
  593.     register char *m = mem + (tocopy = p -> mh_nbytes);
  594.     ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
  595.     ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
  596.   }
  597. #else /* not rcheck */
  598.   if (p -> mh_index >= 13)
  599.     tocopy = (1 << (p -> mh_index + 3)) - ((sizeof *p + 7) & ~7);
  600.   else
  601.     tocopy = p -> mh_size;
  602. #endif /* not rcheck */
  603.  
  604.   /* See if desired size rounds to same power of 2 as actual size. */
  605.   nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
  606.  
  607.   /* If ok, use the same block, just marking its size as changed.  */
  608.   if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
  609.     {
  610. #ifdef rcheck
  611.       register char *m = mem + tocopy;
  612.       *m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
  613.       p-> mh_nbytes = n;
  614.       m = mem + n;
  615.       *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
  616. #else /* not rcheck */
  617.       p -> mh_size = n;
  618. #endif /* not rcheck */
  619.       return mem;
  620.     }
  621.  
  622.   if (n < tocopy)
  623.     tocopy = n;
  624.   {
  625.     register char *new;
  626.  
  627.     if ((new = malloc (n)) == 0)
  628.       return 0;
  629.     memcpy(new, mem, tocopy);
  630.     free (mem);
  631.     return new;
  632.   }
  633. }
  634.  
  635. /* This is in case something linked with Emacs calls calloc.  */
  636.  
  637. char *
  638. calloc (num, size)
  639.      unsigned num, size;
  640. {
  641.   register char *mem;
  642.  
  643.   num *= size;
  644.   mem = malloc (num);
  645.   if (mem != 0)
  646.     memset(mem, 0, num);
  647.   return mem;
  648. }
  649.  
  650. /* This is in case something linked with Emacs calls cfree.  */
  651.  
  652. cfree (mem)
  653.      char *mem;
  654. {
  655.   return free (mem);
  656. }
  657.  
  658. #ifndef VMS
  659.  
  660. char *
  661. memalign (alignment, size)
  662.      unsigned alignment, size;
  663. {
  664.   register char *ptr = malloc (size + alignment);
  665.   register char *aligned;
  666.   register struct mhead *p;
  667.  
  668.   if (ptr == 0)
  669.     return 0;
  670.   /* If entire block has the desired alignment, just accept it.  */
  671.   if (((int) ptr & (alignment - 1)) == 0)
  672.     return ptr;
  673.   /* Otherwise, get address of byte in the block that has that alignment.  */
  674.   aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
  675.  
  676.   /* Store a suitable indication of how to free the block,
  677.      so that free can find the true beginning of it.  */
  678.   p = (struct mhead *) (aligned - ((7 + sizeof (struct mhead)) & ~7));
  679.   p -> mh_size = aligned - ptr;
  680.   p -> mh_alloc = ISMEMALIGN;
  681.   return aligned;
  682. }
  683.  
  684. #ifndef HPUX
  685. /* This runs into trouble with getpagsz on HPUX.
  686.    Patching out seems cleaner than the ugly fix needed.  */
  687. char *
  688. valloc (size)
  689. {
  690.   return memalign (getpagsz (), size);
  691. }
  692. #endif /* not HPUX */
  693. #endif /* not VMS */
  694.  
  695. #ifdef MSTATS
  696. /* Return statistics describing allocation of blocks of size 2**n. */
  697.  
  698. struct mstats_value
  699.   {
  700.     int blocksize;
  701.     int nfree;
  702.     int nused;
  703.   };
  704.  
  705. struct mstats_value
  706. malloc_stats (size)
  707.      int size;
  708. {
  709.   struct mstats_value v;
  710.   register int i;
  711.   register struct mhead *p;
  712.  
  713.   v.nfree = 0;
  714.  
  715.   if (size < 0 || size >= 30)
  716.     {
  717.       v.blocksize = 0;
  718.       v.nused = 0;
  719.       return v;
  720.     }
  721.  
  722.   v.blocksize = 1 << (size + 3);
  723.   v.nused = nmalloc[size];
  724.  
  725.   for (p = nextf[size]; p; p = CHAIN (p))
  726.     v.nfree++;
  727.  
  728.   return v;
  729. }
  730. int
  731. malloc_mem_used ()
  732. {
  733.   int i;
  734.   int size_used;
  735.  
  736.   size_used = 0;
  737.   
  738.   for (i = 0; i < 30; i++)
  739.     {
  740.       int allocation_size = 1 << (i + 3);
  741.       struct mhead *p;
  742.       
  743.       size_used += nmalloc[i] * allocation_size;
  744.     }
  745.  
  746.   return size_used;
  747. }
  748.  
  749. int 
  750. malloc_mem_free ()
  751. {
  752.   int i;
  753.   int size_unused;
  754.  
  755.   size_unused = 0;
  756.   
  757.   for (i = 0; i < 30; i++)
  758.     {
  759.       int allocation_size = 1 << (i + 3);
  760.       struct mhead *p;
  761.       
  762.       for (p = nextf[i]; p ; p = CHAIN (p))
  763.     size_unused += allocation_size;
  764.     }
  765.  
  766.   return size_unused;
  767. }
  768. #endif /* MSTATS */
  769.  
  770. /*
  771.  *    This function returns the total number of bytes that the process
  772.  *    will be allowed to allocate via the sbrk(2) system call.  On
  773.  *    BSD systems this is the total space allocatable to stack and
  774.  *    data.  On USG systems this is the data space only.
  775.  */
  776.  
  777. #ifdef WINDOWSNT
  778. get_lim_data ()
  779. {
  780.   lim_data = 0x7fffffff;    // this is the MAX...
  781. }
  782. #else /* !WINDOWSNT */
  783. #ifdef USG
  784.  
  785. get_lim_data ()
  786. {
  787.   extern long ulimit ();
  788.     
  789. #ifdef ULIMIT_BREAK_VALUE
  790.   lim_data = ULIMIT_BREAK_VALUE;
  791. #else
  792.   lim_data = ulimit (3, 0);
  793. #endif
  794.  
  795.   lim_data -= (long) data_space_start;
  796. }
  797.  
  798. #else /* not USG */
  799. #if defined (BSD4_1) || defined (VMS)
  800.  
  801. get_lim_data ()
  802. {
  803.   lim_data = vlimit (LIM_DATA, -1);
  804. }
  805.  
  806. #else /* not BSD4_1 and not VMS */
  807.  
  808. get_lim_data ()
  809. {
  810.   struct rlimit XXrlimit;
  811.  
  812.   getrlimit (RLIMIT_DATA, &XXrlimit);
  813. #ifdef RLIM_INFINITY
  814.   lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
  815. #else
  816.   lim_data = XXrlimit.rlim_cur;    /* soft limit */
  817. #endif
  818. }
  819.  
  820. #endif /* not BSD4_1 and not VMS */
  821. #endif /* not USG */
  822. #endif /* !WINDOWSNT */
  823.  
  824. #ifdef VMS
  825. /* There is a problem when dumping and restoring things on VMS. Calls
  826.  * to SBRK don't necessarily result in contiguous allocation. Dumping
  827.  * doesn't work when it isn't. Therefore, we make the initial
  828.  * allocation contiguous by allocating a big chunk, and do SBRKs from
  829.  * there. Once Emacs has dumped there is no reason to continue
  830.  * contiguous allocation, malloc doesn't depend on it.
  831.  *
  832.  * There is a further problem of using brk and sbrk while using VMS C
  833.  * run time library routines malloc, calloc, etc. The documentation
  834.  * says that this is a no-no, although I'm not sure why this would be
  835.  * a problem. In any case, we remove the necessity to call brk and
  836.  * sbrk, by calling calloc (to assure zero filled data) rather than
  837.  * sbrk.
  838.  *
  839.  * VMS_ALLOCATION_SIZE is the size of the allocation array. This
  840.  * should be larger than the malloc size before dumping. Making this
  841.  * too large will result in the startup procedure slowing down since
  842.  * it will require more space and time to map it in.
  843.  *
  844.  * The value for VMS_ALLOCATION_SIZE in the following define was determined
  845.  * by running emacs linked (and a large allocation) with the debugger and
  846.  * looking to see how much storage was used. The allocation was 201 pages,
  847.  * so I rounded it up to a power of two.
  848.  */
  849. #ifndef VMS_ALLOCATION_SIZE
  850. #define VMS_ALLOCATION_SIZE    (512*256)
  851. #endif
  852.  
  853. /* Use VMS RTL definitions */
  854. #undef sbrk
  855. #undef brk
  856. #undef malloc
  857. int vms_out_initial = 0;
  858. char vms_initial_buffer[VMS_ALLOCATION_SIZE];
  859. static char *vms_current_brk = vms_initial_buffer;
  860. static char *vms_end_brk = &vms_initial_buffer[VMS_ALLOCATION_SIZE-1];
  861.  
  862. #include <stdio.h>
  863.  
  864. char *
  865. sys_sbrk (incr)
  866.      int incr;
  867. {
  868.   char *sbrk(), *temp, *ptr;
  869.  
  870.   if (vms_out_initial)
  871.     {
  872.       /* out of initial allocation... */
  873.       if (!(temp = (char*) malloc (incr)))
  874.       temp = (char *) -1;
  875.     }
  876.   else
  877.     {
  878.       /* otherwise, go out of our area */
  879.       ptr = vms_current_brk + incr; /* new current_brk */
  880.       if (ptr <= vms_end_brk)
  881.     {
  882.       temp = vms_current_brk;
  883.       vms_current_brk = ptr;
  884.     }
  885.       else
  886.       {
  887.         vms_out_initial = 1;  /* mark as out of initial allocation */
  888.         if (!(temp = (char*) malloc (incr)))
  889.           temp = (char *) -1;
  890.       }
  891.     }
  892.   return temp;
  893. }
  894. #endif /* VMS */
  895.